<template>
  <div class="register_form">
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      :rules="rules"
      label-width="0px"
      status-icon
    >
      <el-form-item prop="nation">
        <el-select
          v-model="ruleForm.nation"
          placeholder="请选择国家"
          filterable
          size="large"
          class="wh"
        >
          <template #prefix>
            <el-icon>
              <MapLocation />
            </el-icon>
          </template>
          <el-option
            v-for="(item, index) in Countries"
            :key="index"
            :label="item.name"
            :value="item.name"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="mobile">
        <el-input
          :prefix-icon="FolderOpened"
          placeholder="请设置邮箱作为登录名"
          size="large"
          v-model="ruleForm.mobile"
        />
      </el-form-item>
      <el-form-item prop="code">
        <div class="flex wh">
          <el-input
            class="mr-lg"
            :prefix-icon="Key"
            placeholder="请输入验证码"
            size="large"
            v-model="ruleForm.code"
          />
          <el-button
            v-if="count <= 0"
            size="large"
            class="flex-1"
            type="primary"
            :loading="countLoading"
            @click="() => setCountDown(60, ruleForm.mobile, 1)"
            >获取验证码</el-button
          >
          <el-button v-else size="large" class="flex-1" type="primary" disabled
            >{{ count }}s后重新获取</el-button
          >
        </div>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          :prefix-icon="Lock"
          placeholder="密码由6-18位数字与字母、符号组成"
          size="large"
          type="password"
          show-password
          v-model="ruleForm.password"
        />
      </el-form-item>
      <el-form-item prop="cPassword">
        <el-input
          :prefix-icon="Lock"
          placeholder="再次输入密码"
          size="large"
          type="password"
          show-password
          v-model="ruleForm.cPassword"
        />
      </el-form-item>
      <el-form-item prop="name">
        <el-input
          :prefix-icon="OfficeBuilding"
          placeholder="公司名称"
          size="large"
          v-model="ruleForm.name"
        />
      </el-form-item>
      <el-form-item class="mt-30">
        <el-button
          :loading="loading"
          @click="handleRegister"
          size="large"
          class="wh"
          type="primary"
          >注 册</el-button
        >
      </el-form-item>
      <el-form-item class="mt-lg mb-30">
        <div class="el-checkbox" @click="handleOpenManual">
          <div
            :class="ruleForm.manual ? 'is-checked' : ''"
            class="el-checkbox__input"
          >
            <div class="el-checkbox__inner"></div>
          </div>
          <div class="el-checkbox__label description">
            <span
              >已阅读并同意 《中国商家个人信息跨境传输同意函》 和
              《平台服务协议》</span
            >
          </div>
        </div>
      </el-form-item>
    </el-form>
    <c-register-agreement
      @change="ruleForm.manual = true"
      ref="cRegisterAgreementRef"
    />
  </div>
</template>
<script setup>
import cRegisterAgreement from "./RegisterAgreement.vue";
import { ref, getCurrentInstance } from "vue";
import Countries from "@/utils/countries.js";
import { useLoginHook } from "@/hooks/useSendVerificationCode.js";
import {
  Iphone,
  Lock,
  Key,
  MapLocation,
  OfficeBuilding,
  FolderOpened,
} from "@element-plus/icons-vue";
import { deepClone } from "@/utils/index";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";
import { setCookies, setLocalStorage } from "@/utils/cache.js";
const { count, countLoading, setCountDown } = useLoginHook();
const router = useRouter();
const { proxy } = getCurrentInstance();
const active = ref(0);
const ruleForm = ref({
  manual: false,
});
const emit = defineEmits(["change"]);
const ruleFormRef = ref();
const loading = ref(false);

const cRegisterAgreementRef = ref();

const handleOpenManual = () => {
  // ruleForm.value.manual = true;
  cRegisterAgreementRef.value.handleOpen();
};

const handleRegister = () => {
  ruleFormRef.value.validate((valid) => {
    if (valid) {
      if (!ruleForm.value.manual) {
        ElMessage.warning("请阅读并同意并遵守 《服务条款》 和 《隐私条款》");
        return;
      }
      const data = deepClone(ruleForm.value);
      loading.value = true;
      proxy.$post({ url: "/buyerReg/email", data }).then(
        (res) => {
          ElMessage.success("注册成功");
          // emit("change");
          setCookies("TOKEN", res.jwt);
          setLocalStorage("USER_INFO", res.user);
          loading.value = false;
          router.push("/");
        },
        () => {
          loading.value = false;
        }
      );
    }
  });
};

const rules = ref({
  nation: [
    {
      required: true,
      message: "请输入国家",
      trigger: "blur",
    },
  ],
  mobile: [
    {
      required: true,
      message: "请输入邮箱",
      trigger: "blur",
    },
  ],
  code: [
    {
      required: true,
      message: "请输入验证码",
      trigger: "blur",
    },
  ],
  password: [
    {
      required: true,
      message: "请输入密码",
      trigger: "blur",
    },
  ],
  cPassword: [
    {
      required: true,
      validator: (rule, value, callback) => {
        if (!value) {
          callback(new Error("请再次确认密码"));
        } else if (ruleForm.value.password !== value) {
          callback(new Error("两次密码输入不一致"));
        } else {
          callback();
        }
      },
      trigger: "blur",
    },
  ],
  name: [
    {
      required: true,
      message: "请输入企业名称",
      trigger: "blur",
    },
  ],
});
</script>
<style lang="less" scoped>
.description {
  display: flex;
  align-items: center;
  line-height: 20px;
  white-space: normal;
}
.register_form {
  margin: 50px 70px 0 70px;
}
</style>